(function() {
    var data = [{
            id: 1,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1570862996.74362387.png?thumb=1&w=200&h=200",
            title: "Fun Home保温泡茶杯（茶水分离） 白色 520ml",
            desc: "茶水分离，长效保温",
            disCount: "65元",
            prcie: "99元"
        },
        {
            id: 2,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1570670535.6211177.jpg?thumb=1&w=200&h=200",
            title: "米家风冷对开门冰箱 483L 银色",
            desc: "风冷双变频，无霜不结冰",
            disCount: "1199元",
            prcie: "2499元"
        },
        {
            id: 3,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1573714512.40746513.jpg?thumb=1&w=200&h=200",
            title: "米家扫拖一体机器人 白色",
            desc: "扫得干净，拖得彻底",
            disCount: "1699元",
            prcie: "1999元"
        },
        {
            id: 4,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1572868360.8284015.jpg?thumb=1&w=200&h=200",
            title: "立式空调A（3匹/变频/超一级能效）",
            desc: "超一级能效巨省电，自清洁",
            disCount: "5599元",
            prcie: "5999元"
        },
        {
            id: 5,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1570862996.74362387.png?thumb=1&w=200&h=200",
            title: "Fun Home保温泡茶杯（茶水分离） 白色 520ml",
            desc: "茶水分离，长效保温",
            disCount: "65元",
            prcie: "99元"
        },
        {
            id: 6,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1570670535.6211177.jpg?thumb=1&w=200&h=200",
            title: "米家风冷对开门冰箱 483L 银色",
            desc: "风冷双变频，无霜不结冰",
            disCount: "1199元",
            prcie: "2499元"
        },
        {
            id: 7,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1573714512.40746513.jpg?thumb=1&w=200&h=200",
            title: "米家扫拖一体机器人 白色",
            desc: "扫得干净，拖得彻底",
            disCount: "1699元",
            prcie: "1999元"
        },
        {
            id: 8,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1572868360.8284015.jpg?thumb=1&w=200&h=200",
            title: "立式空调A（3匹/变频/超一级能效）",
            desc: "超一级能效巨省电，自清洁",
            disCount: "5599元",
            prcie: "5999元"
        },
        {
            id: 9,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1570862996.74362387.png?thumb=1&w=200&h=200",
            title: "Fun Home保温泡茶杯（茶水分离） 白色 520ml",
            desc: "茶水分离，长效保温",
            disCount: "65元",
            prcie: "99元"
        },
        {
            id: 10,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1570670535.6211177.jpg?thumb=1&w=200&h=200",
            title: "米家风冷对开门冰箱 483L 银色",
            desc: "风冷双变频，无霜不结冰",
            disCount: "1199元",
            prcie: "2499元"
        },
        {
            id: 11,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1573714512.40746513.jpg?thumb=1&w=200&h=200",
            title: "米家扫拖一体机器人 白色",
            desc: "扫得干净，拖得彻底",
            disCount: "1699元",
            prcie: "1999元"
        },
        {
            id: 12,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1572868360.8284015.jpg?thumb=1&w=200&h=200",
            title: "立式空调A（3匹/变频/超一级能效）",
            desc: "超一级能效巨省电，自清洁",
            disCount: "5599元",
            prcie: "5999元"
        },
        {
            id: 13,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1570862996.74362387.png?thumb=1&w=200&h=200",
            title: "Fun Home保温泡茶杯（茶水分离） 白色 520ml",
            desc: "茶水分离，长效保温",
            disCount: "65元",
            prcie: "99元"
        },
        {
            id: 14,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1570670535.6211177.jpg?thumb=1&w=200&h=200",
            title: "米家风冷对开门冰箱 483L 银色",
            desc: "风冷双变频，无霜不结冰",
            disCount: "1199元",
            prcie: "2499元"
        },
        {
            id: 15,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1573714512.40746513.jpg?thumb=1&w=200&h=200",
            title: "米家扫拖一体机器人 白色",
            desc: "扫得干净，拖得彻底",
            disCount: "1699元",
            prcie: "1999元"
        },
        {
            id: 16,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1572868360.8284015.jpg?thumb=1&w=200&h=200",
            title: "立式空调A（3匹/变频/超一级能效）",
            desc: "超一级能效巨省电，自清洁",
            disCount: "5599元",
            prcie: "5999元"
        },
        {
            id: 17,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1570862996.74362387.png?thumb=1&w=200&h=200",
            title: "Fun Home保温泡茶杯（茶水分离） 白色 520ml",
            desc: "茶水分离，长效保温",
            disCount: "65元",
            prcie: "99元"
        },
        {
            id: 18,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1570670535.6211177.jpg?thumb=1&w=200&h=200",
            title: "米家风冷对开门冰箱 483L 银色",
            desc: "风冷双变频，无霜不结冰",
            disCount: "1199元",
            prcie: "2499元"
        },
        {
            id: 19,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1573714512.40746513.jpg?thumb=1&w=200&h=200",
            title: "米家扫拖一体机器人 白色",
            desc: "扫得干净，拖得彻底",
            disCount: "1699元",
            prcie: "1999元"
        },
        {
            id: 20,
            imgURL: "https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1572868360.8284015.jpg?thumb=1&w=200&h=200",
            title: "立式空调A（3匹/变频/超一级能效）",
            desc: "超一级能效巨省电，自清洁",
            disCount: "5599元",
            prcie: "5999元"
        }
    ]
    console.log(a)
        //闪购倒计时区域
    var t2 = +new Date() + 7200000

    function getTime() {
        var t4 = 0
        var t3 = 1583143200000
        var tiems1 = document.querySelector(".times")
        var shoping_spn = document.querySelector(".shoping_spn")
        shoping_spn.innerHTML = ""
            //  var ti = localStorage.setItem('time', )
            //   var t1 = +new Date() + 7200000
            // var time = new Date()
            // var y = time.getFullYear() //年
            // var m = time.getMonth() + 1 //月
            // var d = time.getDate() //日
            // d = d < 10 ? '0' + d : d
            // var h = time.getHours() //小时
            // h = h < 10 ? '0' + h : h
            // var ms = time.getMinutes() //分
            // ms = ms < 10 ? '0' + ms : ms
            // var s = time.getSeconds() //秒
            // s = s < 10 ? '0' + s : s
            //  console.log(y, m, d, h, ms, s)

        var time = +new Date()
            // console.log(time, t3)
        if (time < t3) {
            //  console.log(t3)
            var t1 = t3
        } else if (time >= t3) {
            // t4 += 720000
            // t3 = t3 + t4
            // t3 += 7200000
            // var t1 = t3
            // console.log(Math.ceil((time - t3) / 7200000))
            t3 = t3 + 7200000 * Math.ceil((time - t3) / 7200000)
            var t1 = t3
        }
        var times = (t1 - time) / 1000
        var h = parseInt((times / 60 / 60) % 24) //时
        h = h < 10 ? "0" + h : h
        var m = parseInt((times / 60) % 60) //分
        m = m < 10 ? "0" + m : m
        var s = parseInt(times % 60) //秒
        s = s < 10 ? "0" + s : s
        var li = `<span>${h}</span> :
        <span>${m}</span> :
        <span>${s}</span>`
        shoping_spn.insertAdjacentHTML("beforeend", li)
        var t5 = new Date(t3)
        var t6 = t5.getHours()
        t6 = t6 < 10 ? '0' + t6 : t6
        tiems1.innerHTML = `${t6}:00场`

    }
    var h1 = +new Date("2020-3-2 18:00:00") - +new Date("2020-3-2 16:00:00")
        // console.log(new Date() + +new Date(h1))
    getTime()
    setInterval(() => {
        getTime()
    }, 1000)
    Date.prototype.Format = function(fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "H+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            S: this.getMilliseconds() //毫秒
        }
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(
                RegExp.$1,
                (this.getFullYear() + "").substr(4 - RegExp.$1.length)
            )
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(
                    RegExp.$1,
                    RegExp.$1.length == 1 ?
                    o[k] :
                    ("00" + o[k]).substr(("" + o[k]).length)
                )
        return fmt
    }
    var time1 = new Date().Format("2020-3-2 16:00:00")
        //  console.log(time1)
        //闪购区域
    var lis = document.querySelector(".lis")
    var left = document.querySelector("#left")
    var right = document.querySelector("#right")
    var index = 0
    var total = data.length / 4
    left.onclick = function() {
        console.log("向左")
        if (index == 0) {
            return
        }
        index--
        lis.style.transform = "translateX(-" + index * 250 * 4 + "px)"
    }
    right.onclick = function() {
        if (index >= total - 1) {
            return
        }
        index++
        lis.style.transform = "translateX(-" + index * 250 * 4 + "px)"
    }
    getlist(data)

    function getlist(list) {
        var len = list.length
            // console.log(data[19])
        for (var i = 0; i < len; i++) {
            var item = data[i]
                // console.log(item)
            var li = `<div class="li"><img src="${item.imgURL}" alt="">
                    <h3>${item.title}</h3>
                    <p class="price">${item.desc}</p>
                    <p class="p1">
                        <span>${item.disCount}</span>
                        <del>${item.prcie}</del>
                    </p>
                </div>
                    `
            lis.insertAdjacentHTML("beforeend", li)
        }
        len = ""
            // console.log(len)
    }
    var phone = document.querySelector(".phone_fd")
    var items = a.goodsFloorData[2].body.product_list
        //闪购点击跳转区域
    for (var i = 0; i < items.length; i++) {
        var it = items[i]

        // console.log(it)
        var td = `
        <div class="phont_td" data="${it.product_id}">
        <img src="${it.img_url}" alt="">
        <h3>${it.product_name}</h3>
        <p>${it.product_brief}</p>
        <span>${it.product_price}元</span>
    </div>
        `
        phone.insertAdjacentHTML("beforeend", td)
    }
    var ph = document.querySelectorAll(".phont_td")
    for (var i = 0; i < ph.length; i++) {
        ph[i].onclick = function(id) {
            // console.log(this)
            console.log(this.getAttribute("data"))
        }
    }
    //导航栏事件区域
    var Mi
    var Mi1
        //发起导航栏请求数据
    function Miget(ty, mis) {
        fetch("http://dbyxs.xyz:3005/goods/milei/get", {
                method: "POST",
                headers: { "Content-Type": "application/x-www-form-urlencoded" }
                // body: `type=${ty}`
            })
            .then(res => {
                return res.json()
            })
            .then(res => {
                // console.log(res.message)
                Mi = res.message
                Mi.forEach(item => {
                        if (item.type == 2) {
                            //   console.log(item)
                        }
                    })
                    // sessionStorage
                sessionStorage.setItem("obj", JSON.stringify(Mi))
                Mi = Mi.slice(0, 6)
                    //    console.log(Mi[0].img.split(',')[0])
                    // getMdata(Mi)
            })
    }
    Miget(1)

    var tit_nav = document.querySelector(".title_bd").querySelectorAll("span")
    var bd = document.querySelector("#td")
    var tit_bd = document.querySelector(".tit")
        //循环导航栏事件
    for (var i = 0; i < tit_nav.length - 2; i++) {
        var item = i
            // console.log(item)
        tit_nav[i].setAttribute("type", [i + 1])
        tit_nav[i].addEventListener("mouseenter", function() {
            bd.style.display = "block"
            Md(this.getAttribute("type"))
                // Miget(this.getAttribute('type'))
                //   console.log(this.getAttribute("type"))
        })

        bd.addEventListener("mouseleave", function() {
            bd.style.display = "none"
        })
    }
    //渲染导航栏列表
    function Md(al) {
        var val = []
        let obj = JSON.parse(sessionStorage.getItem("obj"))
        obj.forEach(item => {
                if (item.type == al) {
                    // console.log(item)
                    val.push(item)
                    return val.slice(0, 6)
                }
            })
            //console.log(val)
        var li = `
               <div class="tit_bd" data='${val[0].id}'>
               <div class="tit_hd">  
               <img src="${val[0].img.split(",")[0]}"/>        
               <p>${val[0].title}</p>
               <span>${val[0].price}元</span>
           </div>
               </div>
               <div class="tit_bd" data='${val[1].id}'>
               <div class="tit_hd">  
               <img src="${val[1].img.split(",")[0]}"/>        
               <p>${val[1].title}</p>
               <span>${val[1].price}元</span>
           </div>
               </div>
               <div class="tit_bd" data='${val[2].id}'>
               <div class="tit_hd">  
               <img src="${val[2].img.split(",")[0]}"/>        
               <p>${val[2].title}</p>
               <span>${val[2].price}元</span>
           </div>
               </div>
               <div class="tit_bd" data='${val[3].id}'>
               <div class="tit_hd">  
               <img src="${val[3].img.split(",")[0]}"/>        
               <p>${val[3].title}</p>
               <span>${val[3].price}元</span>
           </div>
               </div>
               <div class="tit_bd" data='${val[4].id}'>
               <div class="tit_hd">  
               <img src="${val[4].img.split(",")[0]}"/>        
               <p>${val[4].title}</p>
               <span>${val[4].price}元</span>
           </div>
               </div>
               <div class="tit_bd" data='${val[5].id}'>
               <div class="tit_hd">  
               <img src="${val[5].img.split(",")[0]}"/>        
               <p>${val[5].title}</p>
               <span>${val[5].price}元</span>
           </div>
               </div>
             
                `
            // console.log(Mi[i].title)
            // tit_bd.insertAdjacentHTML("beforeend", li)
        tit_bd.innerHTML = li
            // }
        let tit = document.querySelector(".tit").querySelectorAll(".tit_bd")
        console.log(tit)
        for (var i = 0; i < tit.length; i++) {
            tit[i].addEventListener('click', function() {
                console.log(this.getAttribute('data'))
                location.href = './detail.html?id=' + this.getAttribute('data')
            })
        }
    }
    //导航栏点击跳转
    var tit_bd_s = document.querySelector('.tit').querySelectorAll('.tit_bd')
    console.log(tit_bd_s)
        //搜索框事件
    var inp = document.getElementById("inp")
    var title_gd = document.getElementById("title_gd")
    var btn1 = document.getElementById("btn1")
    inp.onfocus = function() {
        title_gd.style.display = "block"
        inp.setAttribute("class", "in")
        btn1.setAttribute("class", "in")
    }
    inp.onblur = function() {
            title_gd.style.display = "none"
            inp.setAttribute("class", "")
            btn1.setAttribute("class", "")
        }
        //轮播图左侧菜单栏事件
    var banel_hd = document.querySelector(".banel_hd").querySelectorAll("a")
    var ban = document.querySelector(".banel_sd")
    var bans = document.querySelector(".banel_hd")
        //   console.log(banel_hd)
    for (var i = 0; i < banel_hd.length; i++) {
        banel_hd[i].setAttribute("data-it", [i + 1])
        banel_hd[i].addEventListener("mouseenter", function() {
            ban.innerHTML = ""
            console.log(this.getAttribute("data-it"))
            ban.style.zIndex = "99"
            getDa(this.getAttribute("data-it"))
        })
        ban.addEventListener("mouseleave", function() {
            ban.style.zIndex = "-99"
        })
        bans.addEventListener("mouseleave", function() {
            ban.style.zIndex = "-99"
        })
    }
    //开始渲染轮播图左侧菜单栏
    function getDa(al) {
        //  console.log(al)
        let banel_sd = document.querySelector(".banel_sd")

        let val = []
        obj = JSON.parse(sessionStorage.getItem("obj"))
        obj.forEach(item => {
            if (item.type == al) {
                // console.log(item)
                val.push(item)
                return val.slice(0, 6)
            }
        })
        console.log(Math.ceil(val.length / 6))
        banel_sd.style.width = 250 * Math.ceil(val.length / 6) + "px"
        for (var i = 0; i < val.length; i++) {
            //  console.log(val[i])
            let li = `
            <div class="banel_f">
            <img src="${val[i].img.split(",")[0]}" alt="">
            <a href="javascript:;">${val[i].title}</a>
        </div>
            `
            banel_sd.insertAdjacentHTML("beforeend", li)
        }
        // banel_sd.innerHTML = ''
        return
    }
    //唉原生轮播图
    var lunbo = [{
            id: 0,
            url: "../image/b2.webp"
        },
        {
            id: 1,
            url: "../image/b3.webp"
        },
        {
            id: 2,
            url: "../image/b4.webp"
        },
        {
            id: 3,
            url: "../image/b1.jpg"
        }
    ]
    var index = 0
    var banel_bd_m = document.querySelector(".banel_bd")
    var banel_left = document.querySelector(".banel_bd_left")
    var banel_right = document.querySelector(".banel_bd_right")
    var banel_lun = document.querySelector(".banel")
    var banel_bd_li = document.querySelector(".banel_bd_li")
    banel_bd_m.addEventListener("mouseenter", function() {
            banel_left.style.zIndex = "90"
            banel_right.style.zIndex = "90"
        })
        // banel_bd_m.addEventListener("mouseleave", function() {
        //     banel_left.style.zIndex = "-99"
        //     banel_right.style.zIndex = "-99"
        // })

    function getDlun(lunbo) {
        for (var i = 0; i < lunbo.length; i++) {
            let li = `
                <div></div>
            `
            banel_bd_li.insertAdjacentHTML("beforeend", li)
        }
        let banellis = document
            .querySelector(".banel_bd_li")
            .querySelectorAll("div")
        for (var i = 0; i < banellis.length; i++) {
            //  console.log(i)
            banellis[i].setAttribute("data", [i])

            banellis[i].addEventListener("click", function() {
                //   console.log(this.getAttribute('data'))
                for (var i = 0; i < banellis.length; i++) {
                    banellis[i].setAttribute("class", "")
                }
                this.setAttribute("class", "div_li")
                console.log(lunbo)
                index = this.getAttribute("data")
                console.log(index)
                return dex(index)
            })
        }
        return dex(index)
    }
    getDlun(lunbo)

    function getLun(va) {
        // va = va ? '' : 0

        banel_lun.style.backgroundImage = `url(${lunbo[va].url})`
            // console.log(`url(${lunbo[2].url})`)
    }
    getLun(index)

    function dex(va) {
        index = va
        getLun(va)
        let banellis = document
            .querySelector(".banel_bd_li")
            .querySelectorAll("div")
            // console.log(banellis)
        for (var i = 0; i < banellis.length; i++) {
            banellis[i].setAttribute("class", "")
        }
        banellis[va].setAttribute("class", "div_li")
    }
    banel_left.addEventListener("click", function() {
        if (index == 0) {
            dex(lunbo.length - 1)
        } else {
            index--
            // getLun(index)
            dex(index)
        }
    })
    banel_right.addEventListener("click", function() {
        if (index == lunbo.length - 1) {
            dex(0)
        } else {
            index++
            // getLun(index)
            dex(index)
        }
    })
    setInterval(() => {
            if (index < lunbo.length - 1) {
                index++
                dex(index)
                return
            } else if (index == lunbo.length - 1) {
                index = 0
                dex(index)
            }
        }, 5000)
        //右侧滚动栏
    var side_right_r1 = document.querySelector(".side_right_r1")
    var side_right_l1 = document.querySelector(".side_right_l1")
    document.addEventListener("scroll", function(e) {
        var side_right = document.getElementById("side_right")
        var side_right_L = document.getElementById("side_right_L")
        if (window.innerWidth < 1680) {
            side_right.style.display = "none"
            side_right_L.style.display = "block"
        } else if (window.innerWidth >= 1680) {
            console.log("11")
            side_right.style.display = "block"
            side_right_L.style.display = "none"
        }
        // console.log(e)
        if (document.documentElement.scrollTop >= 300) {
            side_right_l1.style.display = "block"
            side_right_r1.style.display = "block"
        } else if (document.documentElement.scrollTop <= 300) {
            side_right_r1.style.display = "none"
            side_right_l1.style.display = "none"
        }
    })
    side_right_r1.addEventListener("click", function() {
            antimon(window, 0)
        })
        //alert(window.innerWidth)
    var side_right_l1 = document.querySelector(".side_right_l1")
    side_right_l1.addEventListener("click", function() {
        antimon(window, 0)
    })
    var side_r = document
        .querySelector(".side_right_L")
        .querySelectorAll(".side_right_l")
        // console.log(side_r[1].childNodes[2].style)
    for (var i = 0; i < side_r.length; i++) {
        side_r[i].addEventListener("mouseenter", function() {
            antim(this.childNodes[2], -100)
        })
        side_r[i].addEventListener("mouseleave", function() {
            antim(this.childNodes[2], 100)
        })
    }
    console.log()
})();
(function() {
    var spn1 = document.querySelector('.spn1')
    var spn2 = document.querySelector('.spn2')
    var mi = localStorage.getItem('mi') || 0
    if (mi != 0) {
        fetch('http://dbyxs.xyz:3005/user/token', {
            method: 'POST',
            headers: {
                "Content-type": "application/json;charset=UTF-8"
            },
            body: JSON.stringify({
                token: mi
            })
        }).then(res => {
            return res.json()
        }).then(res => {
            //console.log(res.message)
            if (res.err == 200) {
                spn2.innerHTML = res.message.username
                    // spn2.innerHTML = sessionStorage.getItem('users')
                spn1.innerHTML = ''
            }
        })
    } else {
        spn2.innerHTML = '登录'
    }
    spn1.addEventListener('click', function() {
        if (spn2.innerHTML == '注册') {
            return
        } else {
            location.href = './login.html'
        }
    })
    spn2.addEventListener('click', function() {
        if (spn2.innerHTML != '登录') {
            return
        } else {
            location.href = './sign.html'
        }
    })
})()